package com.eslserver.dao;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Session;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.eslserver.to.Schedule;
import com.eslserver.to.wrapper.ESLCollectionPageWrapper;
import com.eslserver.util.HibernateUtil;

public class DBScheduleDAO implements ScheduleDAO {

	public static final Logger LOG = LoggerFactory
			.getLogger(DBScheduleDAO.class);

	protected DBScheduleDAO() throws SQLException {
		// Empty protected constructor to avoid unnecessary initialization
	}

	@Override
	public Schedule addSchedule(Schedule schedule) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.save(schedule);
		return schedule;
	}

	@Override
	public void updateSchedule(Schedule schedule) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.update(schedule);
	}

	@Override
	public Schedule loadSchedule(Integer scheduleID) {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Schedule schedule = (Schedule) session.load(Schedule.class, scheduleID);
		return schedule;
	}

	@Override
	public void deleteSchedule(Integer scheduleID) {
		Schedule eslPushSchedule = loadSchedule(scheduleID);
		if (eslPushSchedule != null) {
			eslPushSchedule.setDelete(true);
			updateSchedule(eslPushSchedule);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public ESLCollectionPageWrapper<Schedule> loadScheduleByPageQuery(
			String sQuery, String aQuery) {

		ESLCollectionPageWrapper<Schedule> scheduleCollectionPageWrapper = new ESLCollectionPageWrapper<Schedule>();

		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		Collection<Schedule> scheduleCollection = session
				.createSQLQuery(sQuery).addEntity(Schedule.class).list();

		String foundQuery = aQuery;
		BigInteger foundCount = (BigInteger) session.createSQLQuery(foundQuery)
				.uniqueResult();
		scheduleCollectionPageWrapper.setFilteredTotal(foundCount.intValue());

		String allQuery = "SELECT COUNT(*) FROM esl_push_schedule WHERE isDelete=false";
		BigInteger allCount = (BigInteger) session.createSQLQuery(allQuery)
				.uniqueResult();
		scheduleCollectionPageWrapper.setTotal(allCount.intValue());

		scheduleCollectionPageWrapper.getESLCollection().addAll(
				scheduleCollection);

		return scheduleCollectionPageWrapper;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Collection<Schedule> loadAllSchedules() {
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();

		Schedule schedule = Schedule.createSchedule();
		schedule.setDelete(false);

		Example example = Example.create(schedule);

		Collection<Schedule> scheduleCollection = session
				.createCriteria(Schedule.class).add(example).list();

		return scheduleCollection;
	}

}
